    var current_node;
    function treeSelect(node){
    	current_node=node;
    	var isleaf=$('#grouptree').tree('isLeaf', node.target);
    	var filters=new Array();
    	var ids="";
    	if(node.id==-1){
    		filters.push(add_condition("and", "eq", "", "id", -1, "", "number"));
    		filters.push(add_condition("or", "eq", "", "g_parent", -1, "", "number"));
    	}else if(isleaf){
    		filters.push(add_condition("and", "eq", "", "id", node.id, "", "number"));
    	}else{
    		var children = $('#grouptree').tree('getChildren', node.target); 
    		for(var i=0;i<children.length;i++){
        		ids+=children[i].id+",";
        	}
    		ids=ids+node.id;
    		filters.push(add_condition("and", "in", "", "id", ids, "", "String"));
    	
    	}
		fun_query('groupgrid', filters);
	}
	
/**
 * 双击列表一行
 * @param rowIndex
 * @param rowData
 */	
 function clickGroupRow(rowIndex,rowData){
			 	$('#gdialog').dialog('open');
			  	$('#gfrom').form('clear');		        
			  	$('#gfrom').form('load',{
			  		  id:rowData.id,
			  		  GName:rowData.g_name,
					  GParent:rowData.g_parent,
					  GOrder:rowData.g_order,
					  GDeptno:rowData.g_deptno,
					  GBusiNo:rowData.g_busi_no,
					  GRemarks:rowData.g_remarks
				});	
			}


/**
 * 保存组织架构
 */
	function saveGroup(){
		$('#gfrom').form('submit', {   
		    url:'group/edit.do',   
		    onSubmit: function(){   
				  var isValid = $(this).form('validate');				  
					if (isValid){
						$.messager.progress({
							title:'提示',
							msg:'正在处理请稍后... ... '
						});					
					}					
					return isValid;	
					},   
		    success:function(data){  
		    	$.messager.progress('close'); 
		    	obj=eval('('+data+')');
		    	if(obj.status){
		    		$('#groupgrid').datagrid('reload');
		    		$('#gdialog').dialog('close');	
		    		$.messager.alert('info','保存成功！');
		    		reloadTree();
		    	}else{
		    		$.messager.alert('info','保存失败！');
		    	}
		    }   
		});		
	}
/**
 * 编辑组织架构
 */	
	function editGroup(){
		var rows=getGridSelectRows("groupgrid");
		if(rows.length==1){
			var rowData=rows[0];
			$('#gdialog').dialog('open');
		  	$('#gfrom').form('clear');		        
		  	$('#gfrom').form('load',{
		  		  id:rowData.id,
		  		  GName:rowData.g_name,
				  GParent:rowData.g_parent,
				  GOrder:rowData.g_order,
				  GDeptno:rowData.g_deptno,
				  GRemarks:rowData.g_remarks
			});	
		}else{
			$.messager.alert('info','请选中一条记录！');
		}
	}
/**
 * 删除组织架构
 */	
	function deleteGroup(){
		var rows=getGridSelectRows("groupgrid");
		if(rows.length>0){
			$.messager.confirm('Confirm','是否确认删除选中记录?',function(r){
				if (r){
					var hashSet=new HashSet();
					for (var i=0,len=rows.length; i<len; i++)
					{
						var node = $('#grouptree').tree('find', rows[i].id);
						var isleaf=$('#grouptree').tree('isLeaf', node.target);
						if(isleaf){
				    		hashSet.add(node.id);
				    	}else{
							var children = $('#grouptree').tree('getChildren', node.target); 
							for(var j=0;j<children.length;j++){
								hashSet.add(children[j].id);
					    	}
							hashSet.add(rows[i].id);
				    	}
					}
					var myArray=hashSet.toArray();
					var ids=myArray.join(",");
					
					jQuery.ajax({
						type : "post",
						url : "group/delete.do",
						data:{ids:ids},
						success : function(data) {
							obj=eval('('+data+')');
					    	if(obj.status){
					    		$('#groupgrid').datagrid('reload');
					    		$('#gdialog').dialog('close');	
					    		reloadTree();
					    		$.messager.alert('info','删除成功！');
					    	}else{
					    		$.messager.alert('info','删除失败！');
					    	}
						},
				    	error : function() {
				    		$.messager.alert('info','网络超时，请稍后再试！');
				    	}
					});
				}
			});
		}else{
			$.messager.alert('info','请选中要删除的记录！');
		}
	}
	
	
	/**
	 * 删除组织架构
	 */	
		function disbleGroup(){
			var rows=getGridSelectRows("groupgrid");
			if(rows.length>0){
				$.messager.confirm('Confirm','是否确认禁用选中记录?',function(r){
					if (r){
						var hashSet=new HashSet();
						for (var i=0,len=rows.length; i<len; i++)
						{
							var node = $('#grouptree').tree('find', rows[i].id);
							var isleaf=$('#grouptree').tree('isLeaf', node.target);
							if(isleaf){
					    		hashSet.add(node.id);
					    	}else{
								var children = $('#grouptree').tree('getChildren', node.target); 
								for(var j=0;j<children.length;j++){
									hashSet.add(children[j].id);
						    	}
								hashSet.add(rows[i].id);
					    	}
						}
						var myArray=hashSet.toArray();
						var ids=myArray.join(",");
						
						jQuery.ajax({
							type : "post",
							url : "group/disable.do",
							data:{ids:ids},
							success : function(data) {
								obj=eval('('+data+')');
						    	if(obj.status){
						    		$('#groupgrid').datagrid('reload');
						    		$('#gdialog').dialog('close');	
						    		reloadTree();
						    		$.messager.alert('info','禁用成功！');
						    	}else{
						    		$.messager.alert('info','禁用失败！');
						    	}
							},
					    	error : function() {
					    		$.messager.alert('info','网络超时，请稍后再试！');
					    	}
						});
					}
				});
			}else{
				$.messager.alert('info','请选中要删除的记录！');
			}
		}
	
	/** 
	 * 获取指定表格的选中行 
	 * **/
	function getGridSelectRows(gridid){
		var id="#"+gridid;
		var rows=$(id).datagrid('getSelections');	
		return rows;
	}
	
	
	/** 判断是否只是选中一行 选中一行  返回选中行的ID 选中多行未选中  返回 null **/
	function isOneRow(gridid){
		var rows=getGridSelectRows(gridid);
		var flag=false;
		if(rows != null){
			if(rows.length==1){
				flag=true;
			}
		}
		return flag;
	}
	/**
	 * @param index
	 * @param row
	 * @returns {String}
	 */
	function rowstyle(index,row){
			if (row.g_status=='2'){
				return 'color:red';
			}
		 }
	function openGroup(){
		$('#gfrom').form('clear');	
		$('#gdialog').dialog('open');
	}
	
	
	function closedialog(){
		$('#gdialog').dialog('close')
	}
	
	function reloadTree(){
		$('#grouptree').tree('reload');
		$('#GParent').combotree('reload');
		/**
		 * 正常情况上面reload,就可以了，但是现在不行。想不到有什么更好的解决办法，只好用ajax再去数据库读一遍。
		 */
		$.ajax({
			type : "post",
			url : "group/group_native_tree.tree",
			dataType:"json",
			success : function(data) {
				$('#grouptree').tree('loadData', data);
				$('#GParent').combotree('loadData', data);
			},
	    	error : function() {
	    		$.messager.alert('info','网络超时，请稍后再试！');
	    	}
		});
	}
	
	function groupQuery(){
		var value1=$('#gnameinput').val(); 
		var value2=$('#deptnoinput').val(); 
		
		var filters = [];
		var flag=false;
		if (!value1 || (value1.replace(/(^\s*)|(\s*$)/g, "").length == 0)) {
			// 如果查询参数为空则刷新列表
			
		} else{
			filters.push(add_condition("and", "包含", "", "g_name", value1, "", "string"));
			flag=true;
		}
		
		if(!value2 || (value2.replace(/(^\s*)|(\s*$)/g, "").length == 0)) {
			
		} else{
			filters.push(add_condition("and", "包含", "", "g_deptno", value2, "", "string"));
			flag=true;
		}
			// 查询参数不为空则执行查询
		if(flag){
			fun_query('groupgrid', filters);
		}
	}
	
	function formatStatus(value, row, index){
		if(row.g_status==1){return "<img src=\"res/img/ok.png\"/>";}
		else if(row.g_status==2){
			return "<img src=\"res/img/cross.png\"/>";
		}
	}

	function groupTreeSuccess(node, data){
		$('#GParent').combotree('loadData', data);
		this.onLoadSuccess(node,data); 
	}
	
	
	window.onload=function(){
		console.info("进入数据加载");
		$.ajax({
			type : "post",
			url : "group/group_native_tree.tree",
			dataType:"json",
			success : function(data) {
				$('#grouptree').tree('loadData', data);
				$('#GParent').combotree('loadData', data);
			},
	    	error : function() {
	    		$.messager.alert('info','网络超时，请稍后再试！');
	    	}
		});
	}
